package com.manong.bookstore.user.session;

import java.util.HashMap;
import java.util.Map;

public class UserSession {
	private final String sessionId;
	private final Map<String, Object> attrs = new HashMap<>();
	private long lastAccessTime;

	public UserSession(String sessionId) {
		this.sessionId = sessionId;
		this.updateLastAccessTime();
	}

	public void setAttribute(String key, Object value) {
		this.attrs.put(key, value);
	}

	public Object getAttribute(String key) {
		return this.attrs.get(key);
	}

	public Map<String, Object> getAttrs() {
		return attrs;
	};

	public String getSessionId() {
		return sessionId;
	}


	public long getLastAccessTime() {
		return lastAccessTime;
	}

	public void updateLastAccessTime() {
		this.lastAccessTime = System.currentTimeMillis();
	}

	@Override
	public String toString() {
		return "UserSession{" +
				"sessionId='" + sessionId + '\'' +
				", lastAccessTime=" + lastAccessTime +
				", attrs=" + attrs +
				'}';
	}
}
